package com.example.keshe0337.utils;

import android.icu.text.SimpleDateFormat;

import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

public class TimeConverters {
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    public static String format(Date time){
        // 格式化当前时间为字符串
        return sdf.format(time);
    }

    public static String format(Long time) {
        return sdf.format(new Date(time));
    }

    public static Long parseToLong(String timeStr) {
        try {
            Date date = sdf.parse(timeStr);
            return date.getTime(); // 转为 long 类型
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static Double calculate(Long start, Long end) {
        double s = start;
        double e = end;
        double sum = e - s;
        double hours = sum / 1000 / 60 / 60;

        // 保留一位小数（四舍五入）
        return Math.round(hours * 10) / 10.0;
    }

    public static Long getNewTime() {
        return System.currentTimeMillis();
    }


}
